<template>
  <div v-loading="loading" class="">
    <div class="no-print">
      <el-form
        ref="form"
        :class="isCard ? 'meeting-card' : ''"
        :model="form"
        :rules="rules"
        label-width="120px"
        class="dataForm"
      >
        <el-row style="flex-wrap: wrap;display: flex">
          <el-col :span="12">
            <el-form-item :label="$t('项目名称')" prop="dealId">
              <template>
                <router-link
                  target="_blank"
                  :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
                  class="link_a"
                >{{ form.dealName }}
                </router-link>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('标的公司')" prop="companyId">
              <template>
                <router-link
                  target="_blank"
                  :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
                  class="link_a"
                >{{ form.companyName }}
                </router-link>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('项目负责人')" prop="personCharge">
              <UserSelect v-model="form.personCharge" :placeholder="$t('项目负责人')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('投资主体')" prop="investId">
              <fund-select
                v-if="isInit"
                v-model="form.investId"
                :is-short="true"
                :placeholder="$t('投资主体')"
                @change="onChange"
              />
              <el-select
                v-else
                v-model="form.investId"
                filterable
                clearable
                :placeholder="$t('投资主体')"
                @change="onChange"
              >
                <el-option
                  v-for="item in fundList"
                  :key="item.investorId"
                  :label="item.shortName"
                  :value="item.investorId"
                  :disabled="item.disabled"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="!form.icMeetingId" :span="12">
            <el-form-item :label="$t('预计投资金额') + $t('金额单位')" prop="expectedInvestAmount">
              <Money v-model="form.expectedInvestAmount" :placeholder="$t('预计投资金额')" unit="w" />
            </el-form-item>
          </el-col>
          <el-col v-if="!form.icMeetingId" :span="12">
            <el-form-item :label="$t('投资币种')" prop="investCurrency">
              <dict-select v-model="form.investCurrency" :placeholder="$t('投资币种')" dictionary="currency" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('申请标题')" prop="actTitle">
              <el-input v-model="form.actTitle" show-word-limit maxlength="200" :placeholder="$t('申请标题')" @input="actTitleChange">
              <!--              <template slot="prepend">{{ form.remark + "-" }}</template>-->
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('申请内容')" prop="applyContent">
              <EditorTxt v-if="!loading" ref="editorTxt" v-model="form.applyContent" :disabled="editorDisabled" :height="400" :placeholder="$t('申请内容')" />
            </el-form-item>
          </el-col>
          <el-col v-if="form.meetingType === 'FK'" :span="24">
            <div class="area-header">
              <span class="area-header-title">{{ $t('财务尽调') }}</span>
            </div>
            <el-table v-loading="loading" :data="form.financeList" border tooltip-effect="darkTable">
              <el-table-column :label="$t('机构名称')" align="center" prop="agencyId">
                <template slot-scope="scope">
                  <el-form-item :prop="'financeList.'+scope.$index+'.agencyId'">
                    <el-select v-model="scope.row.agencyId" filterable :placeholder="$t('机构名称')">
                      <el-option
                        v-for="item in getMediationList('KJS')"
                        :key="item.id"
                        :value="item.id"
                        :label="item.mediationShortName"
                      >
                        <span style="float: left;">{{ item.mediationShortName }}</span>
                        <span
                          style="float: right; color: #8492a6; font-size: 13px;text-align: right;width: 30%;white-space: nowrap;overflow: hidden;text-overflow: ellipsis"
                        >{{
                          item.mediationName
                        }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('机构团队')" align="center" prop="agencyTeam">
                <template slot-scope="scope">
                  <el-form-item :prop="'financeList.'+scope.$index+'.agencyTeam'">
                    <el-select v-model="scope.row.agencyTeam" filterable :placeholder="$t('机构团队')">
                      <el-option
                        v-for="item in getMediationTeamList(scope.row.agencyId)"
                        :key="item.id"
                        :value="item.id"
                        :label="item.deptId"
                      />
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('中介报价(万)')" align="center" prop="agencyAmount" width="180">
                <template slot-scope="scope">
                  <el-form-item :prop="'financeList.'+scope.$index+'.agencyAmount'">
                    <money v-model="scope.row.agencyAmount" unit="w" :placeholder="$t('中介报价')" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('是否中标')" align="center" prop="bidFlag">
                <template slot-scope="scope">
                  <el-form-item :prop="'financeList.'+scope.$index+'.bidFlag'">
                    <dict-select
                      v-model="scope.row.bidFlag"
                      dictionary="sys_yes_no"
                      unit="w"
                      :placeholder="$t('是否中标')"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('备注')" align="center" prop="remark">
                <template slot-scope="scope">
                  <el-form-item :prop="'financeList.'+scope.$index+'.remark'">
                    <el-input v-model="scope.row.remark" type="textarea" autosize />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="text"
                    icon="el-icon-delete"
                    @click="deleteDiligence('finance',scope.$index)"
                  >{{ $t('删除') }}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button type="primary" style="margin-top: 5px;" size="small" @click="addDiligence('finance')">{{
              $t('添加')
            }}
            </el-button>
          </el-col>
          <el-col v-if="form.meetingType === 'FK'" :span="24">
            <div class="area-header">
              <span class="area-header-title">{{ $t('法务尽调') }}</span>
            </div>
            <el-table v-loading="loading" :data="form.legalList" border tooltip-effect="darkTable">
              <el-table-column :label="$t('机构名称')" align="center" prop="agencyId">
                <template slot-scope="scope">
                  <el-form-item :prop="'legalList.'+scope.$index+'.agencyId'">
                    <el-select v-model="scope.row.agencyId" filterable :placeholder="$t('机构名称')">
                      <el-option
                        v-for="item in getMediationList('LS')"
                        :key="item.id"
                        :value="item.id"
                        :label="item.mediationShortName"
                      >
                        <span style="float: left">{{ item.mediationShortName }}</span>
                        <span
                          style="float: right; color: #8492a6; font-size: 13px;text-align: right;width: 30%;white-space: nowrap;overflow: hidden;text-overflow: ellipsis"
                        >{{
                          item.mediationName
                        }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('机构团队')" align="center" prop="agencyTeam">
                <template slot-scope="scope">
                  <el-form-item filterable :prop="'legalList.'+scope.$index+'.agencyTeam'">
                    <el-select v-model="scope.row.agencyTeam" :placeholder="$t('机构团队')">
                      <el-option
                        v-for="item in getMediationTeamList(scope.row.agencyId)"
                        :key="item.id"
                        :value="item.id"
                        :label="item.deptId"
                      />
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('中介报价(万)')" align="center" prop="agencyAmount" width="180">
                <template slot-scope="scope">
                  <el-form-item :prop="'legalList.'+scope.$index+'.agencyAmount'">
                    <money v-model="scope.row.agencyAmount" unit="w" :placeholder="$t('中介报价')" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('是否中标')" align="center" prop="bidFlag">
                <template slot-scope="scope">
                  <el-form-item :prop="'legalList.'+scope.$index+'.bidFlag'">
                    <dict-select
                      v-model="scope.row.bidFlag"
                      dictionary="sys_yes_no"
                      unit="w"
                      :placeholder="$t('是否中标')"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('备注')" align="center" prop="remark">
                <template slot-scope="scope">
                  <el-form-item :prop="'legalList.'+scope.$index+'.remark'">
                    <el-input v-model="scope.row.remark" type="textarea" autosize />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="text"
                    icon="el-icon-delete"
                    @click="deleteDiligence('legal',scope.$index)"
                  >{{ $t('删除') }}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button type="primary" style="margin-top: 5px;" size="small" @click="addDiligence('legal')">{{
              $t('添加')
            }}
            </el-button>
          </el-col>
          <el-col v-if="form.meetingType === 'FK'" :span="24">
            <div class="area-header">
              <span class="area-header-title">{{ $t('行业尽调') }}</span>
            </div>
            <el-table v-loading="loading" :data="form.industryList" border tooltip-effect="darkTable">
              <el-table-column :label="$t('机构名称')" align="center" prop="agencyId">
                <template slot-scope="scope">
                  <el-form-item :prop="'industryList.'+scope.$index+'.agencyId'">
                    <el-select v-model="scope.row.agencyId" filterable :placeholder="$t('机构名称')">
                      <el-option
                        v-for="item in getMediationList('YWZX')"
                        :key="item.id"
                        :value="item.id"
                        :label="item.mediationShortName"
                      >
                        <span style="float: left">{{ item.mediationShortName }}</span>
                        <span
                          style="float: right; color: #8492a6; font-size: 13px;text-align: right;width: 30%;white-space: nowrap;overflow: hidden;text-overflow: ellipsis"
                        >{{
                          item.mediationName
                        }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('机构团队')" align="center" prop="agencyTeam">
                <template slot-scope="scope">
                  <el-form-item :prop="'industryList.'+scope.$index+'.agencyTeam'">
                    <el-select v-model="scope.row.agencyTeam" filterable :placeholder="$t('机构团队')">
                      <el-option
                        v-for="item in getMediationTeamList(scope.row.agencyId)"
                        :key="item.id"
                        :value="item.id"
                        :label="item.deptId"
                      />
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('中介报价(万)')" align="center" prop="agencyAmount" width="180">
                <template slot-scope="scope">
                  <el-form-item :prop="'industryList.'+scope.$index+'.agencyAmount'">
                    <money v-model="scope.row.agencyAmount" unit="w" :placeholder="$t('中介报价')" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('是否中标')" align="center" prop="bidFlag">
                <template slot-scope="scope">
                  <el-form-item :prop="'industryList.'+scope.$index+'.bidFlag'">
                    <dict-select
                      v-model="scope.row.bidFlag"
                      dictionary="sys_yes_no"
                      unit="w"
                      :placeholder="$t('是否中标')"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('备注')" align="center" prop="remark">
                <template slot-scope="scope">
                  <el-form-item :prop="'industryList.'+scope.$index+'.remark'">
                    <el-input v-model="scope.row.remark" type="textarea" autosize />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="text"
                    icon="el-icon-delete"
                    @click="deleteDiligence('industry',scope.$index)"
                  >{{ $t('删除') }}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button type="primary" style="margin-top: 5px;" size="small" @click="addDiligence('industry')">
              {{ $t('添加') }}
            </el-button>
          </el-col>
          <el-col v-if="form.meetingType === 'FK'" :span="24">
            <div class="area-header">
              <span class="area-header-title">{{ $t('背景尽调') }}</span>
            </div>
            <el-table v-loading="loading" :data="form.backdropList" border tooltip-effect="darkTable">
              <el-table-column :label="$t('机构名称')" align="center" prop="agencyId">
                <template slot-scope="scope">
                  <el-form-item :prop="'backdropList.'+scope.$index+'.agencyId'">
                    <el-select v-model="scope.row.agencyId" filterable :placeholder="$t('机构名称')">
                      <el-option
                        v-for="item in getMediationList('BJDC')"
                        :key="item.id"
                        :value="item.id"
                        :label="item.mediationShortName"
                      >
                        <span style="float: left">{{ item.mediationShortName }}</span>
                        <span
                          style="float: right; color: #8492a6; font-size: 13px;text-align: right;width: 30%;white-space: nowrap;overflow: hidden;text-overflow: ellipsis"
                        >{{
                          item.mediationName
                        }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('机构团队')" align="center" prop="agencyTeam">
                <template slot-scope="scope">
                  <el-form-item :prop="'backdropList.'+scope.$index+'.agencyTeam'">
                    <el-select v-model="scope.row.agencyTeam" filterable :placeholder="$t('机构团队')">
                      <el-option
                        v-for="item in getMediationTeamList(scope.row.agencyId)"
                        :key="item.id"
                        :value="item.id"
                        :label="item.deptId"
                      />
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('中介报价(万)')" align="center" prop="agencyAmount" width="180">
                <template slot-scope="scope">
                  <el-form-item :prop="'backdropList.'+scope.$index+'.agencyAmount'">
                    <money v-model="scope.row.agencyAmount" unit="w" :placeholder="$t('中介报价')" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('是否中标')" align="center" prop="bidFlag">
                <template slot-scope="scope">
                  <el-form-item :prop="'backdropList.'+scope.$index+'.bidFlag'">
                    <dict-select
                      v-model="scope.row.bidFlag"
                      dictionary="sys_yes_no"
                      unit="w"
                      :placeholder="$t('是否中标')"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('备注')" align="center" prop="remark">
                <template slot-scope="scope">
                  <el-form-item :prop="'backdropList.'+scope.$index+'.remark'">
                    <el-input v-model="scope.row.remark" type="textarea" autosize />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="text"
                    icon="el-icon-delete"
                    @click="deleteDiligence('backdrop',scope.$index)"
                  >{{ $t('删除') }}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button type="primary" style="margin-top: 5px;" size="small" @click="addDiligence('backdrop')">
              {{ $t('添加') }}
            </el-button>
          </el-col>
          <el-col v-if="form.meetingType === 'FK'" :span="24">
            <div class="area-header">
              <span class="area-header-title">{{ $t('其他尽调') }}</span>
            </div>
            <el-table
              v-loading="loading"
              style="margin-bottom: 10px"
              :data="form.otherList"
              border
              tooltip-effect="darkTable"
            >
              <el-table-column :label="$t('机构名称')" align="center" prop="agencyId">
                <template slot-scope="scope">
                  <el-form-item :prop="'otherList.'+scope.$index+'.agencyId'">
                    <el-select v-model="scope.row.agencyId" filterable :placeholder="$t('机构名称')">
                      <el-option
                        v-for="item in getMediationList('other')"
                        :key="item.id"
                        :value="item.id"
                        :label="item.mediationShortName"
                      >
                        <span style="float: left">{{ item.mediationShortName }}</span>
                        <span
                          style="float: right; color: #8492a6; font-size: 13px;text-align: right;width: 30%;white-space: nowrap;overflow: hidden;text-overflow: ellipsis"
                        >{{
                          item.mediationName
                        }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('机构团队')" align="center" prop="agencyTeam">
                <template slot-scope="scope">
                  <el-form-item :prop="'otherList.'+scope.$index+'.agencyTeam'">
                    <el-select v-model="scope.row.agencyTeam" filterable :placeholder="$t('机构团队')">
                      <el-option
                        v-for="item in getMediationTeamList(scope.row.agencyId)"
                        :key="item.id"
                        :value="item.id"
                        :label="item.deptId"
                      />
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('中介报价(万)')" align="center" prop="agencyAmount" width="180">
                <template slot-scope="scope">
                  <el-form-item :prop="'otherList.'+scope.$index+'.agencyAmount'">
                    <money v-model="scope.row.agencyAmount" unit="w" :placeholder="$t('中介报价')" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('是否中标')" align="center" prop="bidFlag">
                <template slot-scope="scope">
                  <el-form-item :prop="'otherList.'+scope.$index+'.bidFlag'">
                    <dict-select
                      v-model="scope.row.bidFlag"
                      dictionary="sys_yes_no"
                      unit="w"
                      :placeholder="$t('是否中标')"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('备注')" align="center" prop="remark">
                <template slot-scope="scope">
                  <el-form-item :prop="'otherList.'+scope.$index+'.remark'">
                    <el-input v-model="scope.row.remark" type="textarea" autosize />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="text"
                    icon="el-icon-delete"
                    @click="deleteDiligence('other',scope.$index)"
                  >{{ $t('删除') }}
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button
              v-if="type !== 'view'"
              type="primary"
              style="margin-top: 5px;"
              size="small"
              @click="addDiligence('other')"
            >{{ $t('添加') }}
            </el-button>
          </el-col>
        </el-row>
        <FileTable
          :business-id="form.dealId"
          :list.sync="form.fileList"
          data-type="mt_meeting"
          :btn-show="!otherBtnHide"
        />
      </el-form>
    </div>
    <div v-if="formList instanceof Array && formList.length > 0" id="print" ref="print" style="display:none;">
      <PageForm :form-list="formList" :model="form" />
    </div>
  </div>
</template>

<script>
import { saveMtMeeting, getMtMeeting } from '@/api/meeting/mtMeeting'
import { getByWfNodeId } from '@/api/deal/form/dealWfDdApply'
import FundSelect from '@/components/Select/fundSelect.vue'
import EditorTxt from '@/components/Tinymce'
import { listDealInvestment } from '@/api/deal/investment/dealInvestment'
import { getFundBase, getFundName } from '@/api/fund/fundBase'
// import { listMediationBase } from '@/api/deal/mediation/mediationBase'
import { listMediationTeam } from '@/api/deal/mediation/mediationTeam/mediationTeam'
import { mapGetters } from 'vuex'

export default {
  components: { EditorTxt, FundSelect },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        investId: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
        personCharge: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
        expectedInvestAmount: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
        investCurrency: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
        actTitle: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        applyContent: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] },
          { validator: this.validateContent, trigger: ['blur', 'change'] }
        ],
        agencyId: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        agencyTeam: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        agencyAmount: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        bidFlag: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }]

      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      otherBtnHide: false,
      isCard: false,
      fundList: [],
      isInit: false,
      sourceTitle: undefined,
      financeList: [],
      legalList: [],
      industryList: [],
      backdropList: [],
      otherList: [],
      teamList: {},
      type: undefined,
      sourceActTitle: '',
      editorDisabled: false,
      formActTitle: '',
      formList: []
    }
  },
  computed: {
    ...mapGetters(['getMediationList', 'getMediationTeamList'])
  },
  created() {
    // this.$bus.$emit('async_done')
    this.$bus.$emit('async_meeting_done')
  },
  methods: {
    getTeamList(id) {
      listMediationTeam({ mediationId: id }).then(res => {
        this.$set(this.teamList, id, res.rows)
      })
    },
    // 初始化数据
    init(data) {
      this.type = data.type
      this.otherBtnHide = data.otherBtnHide
      if (this.type !== 'view') {
        if (this.$parent.$parent.$parent.$parent.$parent.$parent?.hideProcess) {
          this.$parent.$parent.$parent.$parent.$parent.$parent.hideProcess(true)
        }
      }
      if (data.actModifyFrom === 'false') {
        this.editorDisabled = true
      } else {
        this.editorDisabled = false
      }
      this.loading = true
      this.isCard = data && data.isCard
      this.formActTitle = data.actTitle
      if (data != null && data.id || data.meetingId) {
        var id = data.id || data.meetingId
        getMtMeeting(id).then(async response => {
          console.log(response.data.meetingType, '会议类型')
          if (data.meetingType === 'LX' || response.data.meetingType === 'LX') {
            this.isInit = true
          } else {
            this.isInit = false
            var investList = await listDealInvestment({ dealId: response.data.dealId })
            var investment = investList.rows.find(item => item.investorId === response.data.investId)
            if (!investment) {
              var resp = await getFundBase(response.data.investId)
              if (resp.data) {
                var obj = {
                  shortName: resp.data.shortName,
                  investorId: resp.data.id,
                  disabled: true
                }

                if (!(investList.rows instanceof Array)) {
                  investList.rows = []
                }
                investList.rows.push(obj)
              }
            }
            this.fundList = investList.rows
          }

          this.form = response.data
          this.sourceTitle = this.form.remark
          if (!this.form.fileList || this.form.fileList.length === 0) {
            this.$set(this.form, 'fileList', data.fileList)
          } else {
            var fileList = []
            data.fileList.forEach(file => {
              var flag = false
              this.form.fileList.forEach(item => {
                if (file.directoryId === item.directoryId && item.id) {
                  flag = true
                  fileList.push(item)
                }
              })
              if (!flag) {
                fileList.push(file)
              }
            })
            if (!data.fileList || data.fileList.length === 0) {
              fileList = response.data.fileList
            }
            this.form.fileList = fileList
          }
          this.$parent.form = this.form
          this.loading = false
          this.loadForm()
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        getByWfNodeId(data.wfNodeId).then(response => {
          response.data.actTitle = data.actTitle + '-' + this.sourceActTitle
          this.form = response.data
          this.form.investCurrency = 'CNY'
          this.form.fileList = data.fileList
          // this.form.actTitle = data.actTitle
          this.form.remark = data.actTitle
          this.sourceTitle = data.actTitle
          this.form.personCharge = response.data.userId
          this.form.meetingType = data.meetingType
          // 初始化带出数据
          if (data.meetingType === 'LX') {
            this.isInit = true
          } else {
            this.isInit = false
            listDealInvestment({ dealId: this.$route.query.id }).then(response => {
              this.fundList = response.rows
              if (response.rows && response.rows.length > 0) {
                this.$set(this.form, 'investId', response.rows[0].investorId)
                this.$set(this.form, 'expectedInvestAmount', response.rows[0].amountInvestmentEstimate)
                this.onChange()
              }
            })
          }
          this.$parent.form = this.form
          this.loading = false
        })
        this.loading = false
      }
    },
    validateContent(rule, value, cb) {
      var num = this.form.applyContent.length
      try {
        // const text = this.$refs.editorTxt.$el.innerText
        // var temp = text.split('\n')
        // temp = temp[temp.length - 1]
        // num = Number(temp.replace('字', ''))
        num = this.$refs.editorTxt.wordcount

        if (isNaN(Number(num))) {
          num = this.form.applyContent.length
        }
      } catch (e) {
        console.error(e)
        num = this.form.applyContent.length
      }

      if (num > 2000) {
        cb(new Error(this.$t('字数不能超过2000')))
      } else {
        cb()
      }
    },
    loadForm() {
      this.$nextTick(() => {
        this.formList = [
          { title: '申请单', value: 'input', type: 'table',
            tablePageList: [
              { title: '项目名称', type: 'title' },
              { title: '项目名称', value: 'dealName', type: 'input' },
              { title: '标的公司', type: 'title' },
              { title: '标的公司', value: 'companyName', type: 'input' },
              { title: '项目负责人', type: 'title' },
              { title: '项目负责人', value: 'personCharge', type: 'user', userMultiple: false },
              { title: '投资主体', type: 'title', vIf: this.isInit },
              { title: '投资主体', value: 'investId', type: 'fundselect', isShort: true, vIf: this.isInit },
              { title: '投资主体', type: 'title', vIf: !this.isInit },
              { title: '投资主体', value: 'investId', type: 'select', selectType: 'options', selectDataList: this.fundList, selectLabelParams: 'shortName', selectValueParams: 'investorId', vIf: !this.isInit },
              { title: '预计投资金额(万)', type: 'title', moneyUnit: 'w', vIf: !this.form.icMeetingId },
              { title: '预计投资金额(万)', value: 'expectedInvestAmount', type: 'money', moneyUnit: 'w', vIf: !this.form.icMeetingId },
              { title: '投资币种', type: 'title', vIf: !this.form.icMeetingId },
              { title: '投资币种', value: 'investCurrency', type: 'select', selectType: 'dict', dictVal: 'currency', vIf: !this.form.icMeetingId },
              { title: '申请标题', type: 'title' },
              { title: '申请标题', value: 'actTitle', type: 'textarea', width: '75%' },
              { title: '申请内容', type: 'title' },
              { title: '申请内容', value: 'applyContent', type: 'textarea', width: '75%' }
            ]
          },
          { title: '财务尽调', value: 'financeList', type: 'dataList', listType: 'vertical',
            vIf: this.form.financeList.length > 0 && this.form.meetingType === 'FK',
            listPageList: [
              { title: '机构名称', value: 'agencyId', type: 'select', selectOptions: this.getMediationList('KJS'), selectLabelParams: 'mediationShortName', selectValueParams: 'id' },
              { title: '机构团队', value: 'agencyTeam' },
              { title: '中介报价(万)', value: 'agencyAmount', type: 'money', moneyUnit: 'w' },
              { title: '是否中标', value: 'bidFlag', type: 'select', selectType: 'dict', dictVal: 'sys_yes_no' },
              { title: '备注', value: 'remark' }
            ]
          },
          { title: '法务尽调', value: 'legalList', type: 'dataList', listType: 'vertical',
            vIf: this.form.legalList.length > 0 && this.form.meetingType === 'FK',
            listPageList: [
              { title: '机构名称', value: 'agencyId', type: 'select', selectOptions: this.getMediationList('LS'), selectLabelParams: 'mediationShortName', selectValueParams: 'id' },
              { title: '机构团队', value: 'agencyTeam' },
              { title: '中介报价(万)', value: 'agencyAmount', type: 'money', moneyUnit: 'w' },
              { title: '是否中标', value: 'bidFlag', type: 'select', selectType: 'dict', dictVal: 'sys_yes_no' },
              { title: '备注', value: 'remark' }
            ]
          },
          { title: '行业尽调', value: 'industryList', type: 'dataList', listType: 'vertical',
            vIf: this.form.industryList.length > 0 && this.form.meetingType === 'FK',
            listPageList: [
              { title: '机构名称', value: 'agencyId', type: 'select', selectOptions: this.getMediationList('YWZX'), selectLabelParams: 'mediationShortName', selectValueParams: 'id' },
              { title: '机构团队', value: 'agencyTeam' },
              { title: '中介报价(万)', value: 'agencyAmount', type: 'money', moneyUnit: 'w' },
              { title: '是否中标', value: 'bidFlag', type: 'select', selectType: 'dict', dictVal: 'sys_yes_no' },
              { title: '备注', value: 'remark' }
            ]
          },
          { title: '背景尽调', value: 'backdropList', type: 'dataList', listType: 'vertical',
            vIf: this.form.backdropList.length > 0 && this.form.meetingType === 'FK',
            listPageList: [
              { title: '机构名称', value: 'agencyId', type: 'select', selectOptions: this.getMediationList('BJDC'), selectLabelParams: 'mediationShortName', selectValueParams: 'id' },
              { title: '机构团队', value: 'agencyTeam' },
              { title: '中介报价(万)', value: 'agencyAmount', type: 'money', moneyUnit: 'w' },
              { title: '是否中标', value: 'bidFlag', type: 'select', selectType: 'dict', dictVal: 'sys_yes_no' },
              { title: '备注', value: 'remark' }
            ]
          },
          { title: '其他尽调', value: 'otherList', type: 'dataList', listType: 'vertical',
            vIf: this.form.otherList.length > 0 && this.form.meetingType === 'FK',
            listPageList: [
              { title: '机构名称', value: 'agencyId', type: 'select', selectOptions: this.getMediationList('other'), selectLabelParams: 'mediationShortName', selectValueParams: 'id' },
              { title: '机构团队', value: 'agencyTeam' },
              { title: '中介报价(万)', value: 'agencyAmount', type: 'money', moneyUnit: 'w' },
              { title: '是否中标', value: 'bidFlag', type: 'select', selectType: 'dict', dictVal: 'sys_yes_no' },
              { title: '备注', value: 'remark' }
            ]
          },
          { title: '附件信息', value: 'fileList', type: 'fileList', listType: 'vertical',
            vIf: this.form.fileList.length > 0,
            listPageList: [
              { title: '文件分类', value: 'directoryName' },
              { title: '附件名称', value: 'name' },
              { title: '备注', value: 'remark' },
              { title: '上传人', value: 'updateBy', childrenValue: 'nickName' },
              { title: '上传时间', value: 'createTime' }
            ]
          }
        ]
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        emailId: undefined,
        companyId: undefined,
        meetingType: undefined,
        personCharge: undefined,
        investId: undefined,
        expectedInvestAmount: undefined,
        investCurrency: 'CNY',
        actTitle: undefined,
        applyContent: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        financeList: [],
        legalList: [],
        backdropList: [],
        industryList: [],
        otherList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    async onChange() {
      console.log(this.form.investId)
      var name = await getFundName(this.form.investId)
      if (!this.form.id) {
        this.form.remark = name + '-' + this.sourceTitle
        this.form.actTitle = name + '-' + this.sourceTitle + this.sourceActTitle
        this.actTitleChange(this.form.actTitle)
      } else {
        // this.form.remark = name + '-' + this.sourceTitle
        this.form.actTitle = name + '-' + this.formActTitle
        // this.actTitleChange(this.form.actTitle)
      }
    },
    async actTitleChange(value) {
      var name = await getFundName(this.form.investId)
      console.log('---name---', name)
      var preTitle = name + '-' + this.sourceTitle
      if (!this.form.id) {
        if (value.substring(0, preTitle.length) === preTitle) {
          // console.log('员字符串：' + value, '截取的：' + value.substring(preTitle.length), preTitle)
          this.sourceActTitle = value.substring(preTitle.length)
        }
      }
    },
    /** 提交按钮 */
    submitForm() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveMtMeeting(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                resolve(response)
                this.loading = false
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            })
          } else {
            this.locationError(document)
          }
        })
      }).catch(() => {
        this.loading = false
      })
    },
    // 启动流程
    startProcess(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.$t(this.form.actTitle)
      // 业务表名
      processData.dataTable = this.$t('mt_meeting')
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/deal/mt/mtMeetingForm'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.$t('菜单标识')
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    },
    deleteDiligence(type, index) {
      if (!(this.form.financeList instanceof Array)) {
        this.$set(this.form, 'financeList', [])
      }
      if (!(this.form.legalList instanceof Array)) {
        this.$set(this.form, 'legalList', [])
      }
      if (!(this.form.industryList instanceof Array)) {
        this.$set(this.form, 'industryList', [])
      }
      if (!(this.form.backdropList instanceof Array)) {
        this.$set(this.form, 'backdropList', [])
      }
      if (!(this.form.otherList instanceof Array)) {
        this.$set(this.form, 'otherList', [])
      }
      switch (type) {
        case 'finance':
          this.form.financeList.splice(index, 1)
          break
        case 'legal':
          this.form.legalList.splice(index, 1)
          break
        case 'industry':
          this.form.industryList.splice(index, 1)
          break
        case 'backdrop':
          this.form.backdropList.splice(index, 1)
          break
        case 'other':
          this.form.otherList.splice(index, 1)
      }
    },
    // 添加尽调机构
    addDiligence(type) {
      console.log(type)
      if (!(this.form.financeList instanceof Array)) {
        this.$set(this.form, 'financeList', [])
      }
      if (!(this.form.legalList instanceof Array)) {
        this.$set(this.form, 'legalList', [])
      }
      if (!(this.form.industryList instanceof Array)) {
        this.$set(this.form, 'industryList', [])
      }
      if (!(this.form.backdropList instanceof Array)) {
        this.$set(this.form, 'backdropList', [])
      }
      if (!(this.form.otherList instanceof Array)) {
        this.$set(this.form, 'otherList', [])
      }
      var obj = {
        id: undefined,
        diligenceType: type,
        agencyId: undefined,
        agencyTeam: undefined,
        agencyAmount: undefined,
        bidFlag: undefined
      }
      switch (type) {
        case 'finance':
          this.form.financeList.push(obj)
          break
        case 'legal':
          this.form.legalList.push(obj)
          break
        case 'industry':
          this.form.industryList.push(obj)
          break
        case 'backdrop':
          this.form.backdropList.push(obj)
          break
        case 'other':
          this.form.otherList.push(obj)
      }
    }

  }

}
</script>
<style scoped lang="scss">
/deep/ .dataForm .avatar-uploader {
  display: none;
}

.area-header {
  margin-top: 10px;
}

/deep/ .dataForm .el-row .el-form-item {
  min-height: unset;
}

/deep/ .dataForm .el-row .el-form-item.is-error {
  min-height: 54px;
}
</style>
